package TTEST;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

//import panel.CommandPanel;
//import zz.ComboBoxDemo;
import Files.File_;
import a.jframe.ActionListenerPanel;
import a.jframe.Lab;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.InteractiveCallback;
import ch.ethz.ssh2.KnownHosts;
import ch.ethz.ssh2.ServerHostKeyVerifier;
import ch.ethz.ssh2.Session;

public class SwingShell extends JInternalFrame
{

    /* 
     * NOTE: to get this feature to work, replace the "tilde" with your home directory,
     * at least my JVM does not understand it. Need to check the specs.
     */

    static final String knownHostPath = "~/.ssh/known_hosts";
    static final String idDSAPath = "~/.ssh/id_dsa";
    static final String idRSAPath = "~/.ssh/id_rsa";

    static JFrame loginFrame = null;
    JLabel hostLabel;
    JLabel userLabel;
    JTextField hostField;
    JTextField userField;
    JButton loginButton,l2;
    JTextArea terminalArea1;

    static KnownHosts database = new KnownHosts();

    public SwingShell()
    {
            File knownHostFile = new File(knownHostPath);
            if (knownHostFile.exists())
            {
                    try
                    {
                            database.addHostkeys(knownHostFile);
                    }
                    catch (IOException e)
                    {
                    }
            }
            this.startGUI();
    }

    /**
     * This dialog displays a number of text lines and a text field.
     * The text field can either be plain text or a password field.
     */
    class EnterSomethingDialog extends JDialog
    {
            private static final long serialVersionUID = 1L;

            JTextField answerField;
            JPasswordField passwordField;

            final boolean isPassword;

            String answer;

            public EnterSomethingDialog(JFrame parent, String title, String content, boolean isPassword)
            {
                    this(parent, title, new String[] { content }, isPassword);
            }

            public EnterSomethingDialog(JFrame parent, String title, String[] content, boolean isPassword)
            {
                    super(parent, title, true);

                    this.isPassword = isPassword;

                    JPanel pan = new JPanel();
                    pan.setLayout(new BoxLayout(pan, BoxLayout.Y_AXIS));

                    for (int i = 0; i < content.length; i++)
                    {
                            if ((content[i] == null) || (content[i] == ""))
                                    continue;
                            JLabel contentLabel = new JLabel(content[i]);
                            pan.add(contentLabel);

                    }

                    answerField = new JTextField(20);
                    passwordField = new JPasswordField(20);

                    if (isPassword)
                            pan.add(passwordField);
                    else
                            pan.add(answerField);

                    KeyAdapter kl = new KeyAdapter()
                    {
                            public void keyTyped(KeyEvent e)
                            {
                                    if (e.getKeyChar() == '\n')
                                            finish();
                            }
                    };

                    answerField.addKeyListener(kl);
                    passwordField.addKeyListener(kl);

                    getContentPane().add(BorderLayout.CENTER, pan);

                    setResizable(false);
                    pack();
                    setLocationRelativeTo(null);
            }

            private void finish()
            {
                    if (isPassword)
                            answer = new String(passwordField.getPassword());
                    else
                            answer = answerField.getText();

                    dispose();
            }
    }
////////////////////////////////////
    /**
     * TerminalDialog is probably the worst terminal emulator ever written - implementing
     * a real vt100 is left as an exercise to the reader, i.e., to you =)
     *
     */
    int x_=0,y_=0;
    
     class RemoteConsumerOK extends Thread
    {
            char[][] lines1 = new char[y_][];
            int posy1 = 0;
            int posx1 = 0;
            private void addText1(byte[] data, int len)
            {
                    String h="";
                    for (int i = 0; i < len; i++)
                    {
                            
                            char c = (char) (data[i] & 0xff);h+=c;
                            System.out.println(c);
                            if (c == 8) // Backspace, VERASE
                            {
                                    if (posx1 < 0)
                                            continue;
                                    posx1--;
                                    continue;
                            }
                            if (c == '\r')
                            {
                                    posx1 = 0;
                                    continue;
                            }
                            if (c == '\n')
                            {
                                    posy1++;
                                    if (posy1 >= y_)
                                    {
                                            for (int k = 1; k < y_; k++)
                                                    lines1[k - 1] = lines1[k];
                                            posy1--;
                                            lines1[y_ - 1] = new char[x_];
                                            for (int k = 0; k < x_; k++)
                                                    lines1[y_ - 1][k] = ' ';
                                    }
                                    continue;
                            }
                            if (c < 32)
                            {
                                    continue;
                            }
                            if (posx1 >= x_)
                            {
                                    posx1 = 0;
                                    posy1++;
                                    /*if (posy1 >= y_)
                                    {
                                            posy1--;
                                            for (int k = 1; k < y_; k++)
                                                    lines1[k - 1] = lines1[k];
                                            lines1[y_ - 1] = new char[x_];
                                            for (int k = 0; k < x_; k++)
                                                    lines1[y_ - 1][k] = ' ';
                                    }*/
                            }
                            /*if (lines1[posy1] == null)
                            {
                                    lines1[posy1] = new char[x_];
                                    for (int k = 0; k < x_; k++)
                                            lines1[posy1][k] = ' ';
                            }*/
                            //lines1[posy1][posx1] = c;
                            posx1++;
                            pr(c);
                    }
                    StringBuffer sb1 = new StringBuffer(x_ * y_);
                    for (int i = 0; i < lines1.length; i++)
                    {
                            if (i != 0)
                                    sb1.append('\n');
                            if (lines1[i] != null)
                            {
                                    sb1.append(lines1[i]);
                            }
                    }
                    //setContent1(sb1.toString());
                    setContent1(h);
            }
            public void pr(char c){
                    terminalArea1.setText(Character.toString(c));
            }
            public void setContent1(String lines)
            {
                    // setText is thread safe, it does not have to be called from
                    // the Swing GUI thread.
                    System.out.println("tuuu"+lines);
                    terminalArea1.setText(lines);
                    System.out.println(">"+terminalArea1.getText()+"<");
            }
            public void run()
            {
                    System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                    byte[] buff1 = new byte[8192];
                    try
                    {
                            while (true)
                            {
                                    int len1 = in1.read(buff1);
                                    System.out.println(".............."+len1);
                                    if (len1 == -1)
                                            return;
                                    addText1(buff1, len1);
                            }
                    }
                    catch (Exception e)
                    {
                    }
            }
    }
            class RemoteConsumer1 extends Thread
            {
                    char[][] lines = new char[y][];
                    int posy = 0;
                    int posx = 0;

                    private void addText(byte[] data, int len)
                    {
                            for (int i = 0; i < len; i++)
                            {
                                    char c = (char) (data[i] & 0xff);
                                    if (c == 8) // Backspace, VERASE
                                    {
                                            if (posx < 0)
                                                    continue;
                                            posx--;
                                            continue;
                                    }
                                    if (c == '\r')
                                    {
                                            posx = 0;
                                            continue;
                                    }
                                    if (c == '\n')
                                    {
                                            posy++;
                                            if (posy >= y)
                                            {
                                                    for (int k = 1; k < y; k++)
                                                            lines[k - 1] = lines[k];
                                                    posy--;
                                                    lines[y - 1] = new char[x];
                                                    for (int k = 0; k < x; k++)
                                                            lines[y - 1][k] = ' ';
                                            }
                                            continue;
                                    }

                                    if (c < 32)
                                    {
                                            continue;
                                    }
                                    if (posx >= x)
                                    {
                                            posx = 0;
                                            posy++;
                                            if (posy >= y)
                                            {
                                                    posy--;
                                                    for (int k = 1; k < y; k++)
                                                            lines[k - 1] = lines[k];
                                                    lines[y - 1] = new char[x];
                                                    for (int k = 0; k < x; k++)
                                                            lines[y - 1][k] = ' ';
                                            }
                                    }
                                    if (lines[posy] == null)
                                    {
                                            lines[posy] = new char[x];
                                            for (int k = 0; k < x; k++)
                                                    lines[posy][k] = ' ';
                                    }
                                    lines[posy][posx] = c;
                                    posx++;
                            }
                            StringBuffer sb = new StringBuffer(x * y);
                            for (int i = 0; i < lines.length; i++)
                            {
                                    if (i != 0)
                                            sb.append('\n');
                                    if (lines[i] != null)
                                    {
                                            sb.append(lines[i]);
                                    }
                            }
                            terminalArea1.setText(sb.toString());
                            //setContent(sb.toString());
                    }
                    public void run()
                    {
                            byte[] buff = new byte[8192];
                            try
                            {
                                    while (true)
                                    {
                                            int len = in1.read(buff);
                                            if (len == -1)
                                                    return;
                                            addText(buff, len);
                                    }
                            }
                            catch (Exception e)
                            {
                            }
                    }
            }
            int x, y;
    class TerminalDialog extends JTabbedPane//JDialog
    //class TerminalDialog //extends JDialog
    {
            private static final long serialVersionUID = 1L;

            JPanel botPanel;
            JButton logoffButton;
            JTextArea terminalArea;

            Session sess;
            InputStream in;
            OutputStream out;

            int x, y;
            class RemoteConsumer extends Thread
            {
                    char[][] lines = new char[y][];
                    int posy = 0;
                    int posx = 0;

                    private void addText(byte[] data, int len)
                    {
                            for (int i = 0; i < len; i++)
                            {
                                    char c = (char) (data[i] & 0xff);
                                    if (c == 8) // Backspace, VERASE
                                    {
                                            if (posx < 0)
                                                    continue;
                                            posx--;
                                            continue;
                                    }
                                    if (c == '\r')
                                    {
                                            posx = 0;
                                            continue;
                                    }
                                    if (c == '\n')
                                    {
                                            posy++;
                                            if (posy >= y)
                                            {
                                                    for (int k = 1; k < y; k++)
                                                            lines[k - 1] = lines[k];
                                                    posy--;
                                                    lines[y - 1] = new char[x];
                                                    for (int k = 0; k < x; k++)
                                                            lines[y - 1][k] = ' ';
                                            }
                                            continue;
                                    }

                                    if (c < 32)
                                    {
                                            continue;
                                    }
                                    if (posx >= x)
                                    {
                                            posx = 0;
                                            posy++;
                                            if (posy >= y)
                                            {
                                                    posy--;
                                                    for (int k = 1; k < y; k++)
                                                            lines[k - 1] = lines[k];
                                                    lines[y - 1] = new char[x];
                                                    for (int k = 0; k < x; k++)
                                                            lines[y - 1][k] = ' ';
                                            }
                                    }
                                    if (lines[posy] == null)
                                    {
                                            lines[posy] = new char[x];
                                            for (int k = 0; k < x; k++)
                                                    lines[posy][k] = ' ';
                                    }
                                    lines[posy][posx] = c;
                                    posx++;
                            }
                            StringBuffer sb = new StringBuffer(x * y);
                            for (int i = 0; i < lines.length; i++)
                            {
                                    if (i != 0)
                                            sb.append('\n');
                                    if (lines[i] != null)
                                    {
                                            sb.append(lines[i]);
                                    }
                            }
                            setContent(sb.toString(),aa);
                    }
                     
                    JTextArea aa;
                    public RemoteConsumer(JTextArea pp)
                    {
                            aa=pp;
                            //aa.setPreferredSize(new Dimension(50,50));
                    }
                    public void run()
                    {
                            byte[] buff = new byte[8192];
                            try
                            {
                                    while (true)
                                    {
                                            int len = in.read(buff);
                                            if (len == -1)
                                                    return;
                                            addText(buff, len);
                                    }
                            }
                            catch (Exception e)
                            {
                            }
                    }
            }

            //public TerminalDialog(JFrame parent, String title, Session sess, int x, int y) throws IOException
            public TerminalDialog( String title, Session sess, int x, int y) throws IOException
            {
                    //super(parent, title, true);

                    this.sess = sess;

                    in = sess.getStdout();
                    out = sess.getStdin();

                    this.x = x;
                    this.y = y;
/*
                    botPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));

                    logoffButton = new JButton("Logout");
                    botPanel.add(logoffButton);

                    logoffButton.addActionListener(new ActionListener()
                    {
                            public void actionPerformed(ActionEvent e)
                            {
                                     Dispose the dialog, "setVisible(true)" method will return 
                                    //dispose();
                            }
                    });
*/
                    Font f = new Font("Monospaced", Font.PLAIN, 12);

                    terminalArea = new JTextArea(y, x);
                    //terminalArea = new JTextArea(, x);
                    terminalArea.setSize(200, 200);
                    terminalArea.setFont(f);
                    terminalArea.setBackground(Color.BLACK);
                    terminalArea.setForeground(Color.ORANGE);
                    terminalArea.setCaretColor(Color.BLACK);
                    KeyAdapter kl = new KeyAdapter()
                    {
                            public void keyTyped(KeyEvent e)
                            {
                                    int c = e.getKeyChar();

                                    try
                                    {
                                            out.write(c);
                                    }
                                    catch (IOException e1)
                                    {
                                    }
                                    e.consume();
                            }
                    };

                    terminalArea.addKeyListener(kl);
/*
                    //getContentPane().
                    add(terminalArea, BorderLayout.CENTER);
                    //getContentPane().
                    add(botPanel, BorderLayout.PAGE_END);

                    //setResizable(false);
                    //pack();
                    //setLocationRelativeTo(parent);
*/
                    botPanel = new JPanel();
                    
                    MouseListener mouseListener = new MouseAdapter() {
                        public void mouseClicked(MouseEvent e) {
                           System.out.println("zzzzzzzzzzzzzzz"+e.getSource());
                           
                           terminalArea.setText(
                                       terminalArea.getText()+((JList)e.getSource()).getSelectedValue());
                           //
                           //
                           //
                           //obsluga pola z poleceniami
                           //
                           //
                           //
                        }
                    };
    
                    GridLayout experimentLayout = new GridLayout(1,0);
                    //botPanel.setLayout(experimentLayout);
                            JList           listbox=new JList();listbox.setPreferredSize(new Dimension(50,600));
                            DefaultListModel df= new DefaultListModel ();
                            
                            Map<String,String> c= new File_().readF(0);
                            if (!c.isEmpty()){
                                    df.clear();
                            Set<String> test = c.keySet();
                            for (String key : test) {
                                    df.addElement(c.get(key));
                                }
                            }
                            listbox.setModel(df);   listbox.setSize(400,400);
                            listbox.addMouseListener(mouseListener);
                    //      listbox.a
                            
                            
                    //botPanel.add(terminalArea,BorderLayout.PAGE_START);
                    botPanel.add(terminalArea);//,BorderLayout.PAGE_START);
                    botPanel.add(listbox,BorderLayout.PAGE_START);
                    JButton button = new JButton("5 (LINE_END)9999");
                    botPanel.add(button,BorderLayout.PAGE_START);
                    
                   // arraycomandpanel.put("labtest", new CommandPanel("Test"));
            //      botPanel.add(arraycomandpanel.get("labtest"),BorderLayout.CENTER);
                    
                    tabbedPane.addTab( "Page 12", botPanel );
                    loginFrame.add(tabbedPane);

                    new RemoteConsumer(terminalArea).start();
                    //new RemoteConsumer(terminalArea).start();
            }

            public void setContent(String lines,JTextArea pole)
            {
                    pole.setText(lines);
                    //terminalArea1.setText(lines);
            }
    }
//////////////////////////////
    //public static Map<String,CommandPanel> arraycomandpanel=new HashMap<String,CommandPanel>();
    class AdvancedVerifier implements ServerHostKeyVerifier
    {
            public boolean verifyServerHostKey(String hostname, int port, String serverHostKeyAlgorithm,
                            byte[] serverHostKey) throws Exception
            {
                    final String host = hostname;
                    final String algo = serverHostKeyAlgorithm;
                    return true;
            }
    }
    class InteractiveLogic implements InteractiveCallback
    {
            int promptCount = 0;
            String lastError;
            public InteractiveLogic(String lastError)
            {
                    this.lastError = lastError;
            }
            public String[] replyToChallenge(String name, String instruction, int numPrompts, String[] prompt,
                            boolean[] echo) throws IOException
            {
                    String[] result = new String[numPrompts];

                    for (int i = 0; i < numPrompts; i++)
                    {
                            String[] content = new String[] { lastError, name, instruction, prompt[i] };
                            if (lastError != null)
                            {
                                    lastError = null;
                            }
                            EnterSomethingDialog esd = new EnterSomethingDialog(loginFrame, "Keyboard Interactive Authentication",
                                            content, !echo[i]);
                            esd.setVisible(true);
                            if (esd.answer == null)
                                    throw new IOException("Login aborted by user");

                            result[i] = esd.answer;
                            promptCount++;
                    }

                    return result;
            }
            public int getPromptCount()
            {
                    return promptCount;
            }
    }
    class ConnectionThread extends Thread
    {
            String hostname;
            String username;

            public ConnectionThread(String hostname, String username)
            {
                    this.hostname = hostname;
                    this.username = username;
            }

            public void run()
            {
                    Connection conn = new Connection(hostname);

                    try
                    {
                            String[] hostkeyAlgos = database.getPreferredServerHostkeyAlgorithmOrder(hostname);

                            if (hostkeyAlgos != null)
                                    conn.setServerHostKeyAlgorithms(hostkeyAlgos);

                            conn.connect(new AdvancedVerifier());
                            boolean enableKeyboardInteractive = true;
                            boolean enableDSA = true;
                            boolean enableRSA = true;

                            String lastError = null;

                            while (true)
                            {
                                    if ((enableDSA || enableRSA) && conn.isAuthMethodAvailable(username, "publickey"))
                                    {
                                            if (enableDSA)
                                            {
                                                    File key = new File(idDSAPath);

                                                    if (key.exists())
                                                    {
                                                            EnterSomethingDialog esd = new EnterSomethingDialog(loginFrame, "DSA Authentication",
                                                                            new String[] { lastError, "Enter DSA private key password:" }, true);
                                                            esd.setVisible(true);

                                                            boolean res = conn.authenticateWithPublicKey(username, key, esd.answer);

                                                            if (res == true)
                                                                    break;

                                                            lastError = "DSA authentication failed.";
                                                    }
                                                    enableDSA = false; // do not try again
                                            }

                                            if (enableRSA)
                                            {
                                                    File key = new File(idRSAPath);

                                                    if (key.exists())
                                                    {
                                                            EnterSomethingDialog esd = new EnterSomethingDialog(loginFrame, "RSA Authentication",
                                                                            new String[] { lastError, "Enter RSA private key password:" }, true);
                                                            esd.setVisible(true);

                                                            //boolean res = conn.authenticateWithPublicKey(username, key, esd.answer);
                                                            boolean res = conn.authenticateWithPublicKey(username, key, esd.answer);

                                                            if (res == true)
                                                                    break;

                                                            lastError = "RSA authentication failed.";
                                                    }
                                                    enableRSA = false; // do not try again
                                            }

                                            continue;
                                    }
/*
                                    if (enableKeyboardInteractive && conn.isAuthMethodAvailable(username, "keyboard-interactive"))
                                    {
                                            InteractiveLogic il = new InteractiveLogic(lastError);

                                            boolean res = conn.authenticateWithKeyboardInteractive(username, il);

                                            if (res == true)
                                                    break;

                                            if (il.getPromptCount() == 0)
                                            {
                                                    lastError = "Keyboard-interactive does not work.";
                                                    enableKeyboardInteractive = false; // do not try this again
                                            }
                                            else
                                            {
                                                    lastError = "Keyboard-interactive auth failed."; // try again, if possible
                                            }
                                            continue;
                                    }*/
                                    if (conn.isAuthMethodAvailable(username, "password"))
                                    {
                                            /*final EnterSomethingDialog esd = new EnterSomethingDialog(loginFrame,
                                                            "Password Authentication",
                                                            new String[] { lastError, "Enter password for " + username }, true);
                                            esd.setVisible(true);
                                            if (esd.answer == null)
                                                    throw new IOException("Login aborted by user");*/
                                            boolean res = conn.authenticateWithPassword(username, "log");
                                            if (res == true)
                                                    break;
                                            lastError = "Password authentication failed."; // try again, if possible
                                            continue;
                                    }
                                    throw new IOException("No supported authentication methods available.");
                            }
                            Session sess = conn.openSession();
                            int x_width = 130;
                            int y_width = 40;
                            sess.requestPTY("dumb", x_width, y_width, 0, 0, null);
                            sess.startShell();
                            //TerminalDialog td = new TerminalDialog(_frame, username + "@" + hostname, sess, x_width, y_width);
                            TerminalDialog td = new TerminalDialog(username + "@" + hostname, sess, x_width, y_width);
                            td.setSize(200, 100);
                            loginPanel.add(td);
    
                            /*TerminalDialog td1 = new TerminalDialog(username + "@" + hostname, sess, x_width, y_width);
                            td1.setSize(100, 100);
                            loginPanel.add(td1);*/
//                          /td.setVisible(true);
                    }
                    catch (IOException e)
                    {
                            //e.printStackTrace();
                            JOptionPane.showMessageDialog(loginFrame, "Exception: " + e.getMessage());
                    }
                    /*conn.close();
                    Runnable r = new Runnable()
                    {
                            public void run()
                            {
                                    loginFrame.dispose();
                            }
                    };

                    SwingUtilities.invokeLater(r);*/
            }
    }
    
///////////////////////////////
    void loginPressed()
    {
            JDesktopPane jd=new JDesktopPane();
            
            String hostname = hostField.getText().trim();
            String username = userField.getText().trim();

            if ((hostname.length() == 0) || (username.length() == 0))
            {
                    JOptionPane.showMessageDialog(loginFrame, "Please fill out both fields!");
                    return;
            }

            loginButton.setEnabled(false);
            hostField.setEnabled(false);
            userField.setEnabled(false);
            int x_width = 90;
            int y_width = 30;
            //Connect();
            //ConnectionThread_ ct = new ConnectionThread_(hostname, username,database,idDSAPath,idRSAPath,loginPanel, loginFrame);
            liczba++;
            
            _ConnectionThread ct = new _ConnectionThread(hostname, username);
            mapka.put(liczba, ct);
            mapka.get(liczba).start();
            //ct.start();
            
            
            
            ////////////////////
            //_ConnectionThread _ct=new _ConnectionThread(hostname, username);_ct.start();
            ///////////////////
            //System.out.println("ookk");
    }
    static int liczba=0;
    Map<Integer,_ConnectionThread> mapka=new HashMap<Integer, _ConnectionThread>();
    boolean polaczenie=true;
    String hostname;
    String username;
    String hostname1;
    String username1;
    Session sesja;
    InputStream in1;
    OutputStream out1;
    static int x1, y1;
    
    void Connect( )
    {
            hostname="10.41.93.147";
            username="log";
            Connection conn = new Connection(hostname);
            try
            {
                    String[] hostkeyAlgos = database.getPreferredServerHostkeyAlgorithmOrder(hostname);

                    if (hostkeyAlgos != null)
                            conn.setServerHostKeyAlgorithms(hostkeyAlgos);
                    conn.connect(new AdvancedVerifier());
                    boolean enableKeyboardInteractive = true;
                    boolean enableDSA = true;
                    boolean enableRSA = true;
                    String lastError = null;
                    while (true)
                    {
                            if ((enableDSA || enableRSA) && conn.isAuthMethodAvailable(username, "publickey"))
                            {
                                    if (enableDSA)
                                    {
                                            File key = new File(idDSAPath);

                                            if (key.exists())
                                            {
                                                    EnterSomethingDialog esd = new EnterSomethingDialog(loginFrame, "DSA Authentication",
                                                                    new String[] { lastError, "Enter DSA private key password:" }, true);
                                                    esd.setVisible(true);
                                                    boolean res = conn.authenticateWithPublicKey(username, key, esd.answer);
                                                    if (res == true)
                                                            break;
                                                    lastError = "DSA authentication failed.";
                                            }
                                            enableDSA = false; // do not try again
                                    }
                                    if (enableRSA)
                                    {
                                            File key = new File(idRSAPath);
                                            if (key.exists())
                                            {
                                                    EnterSomethingDialog esd = new EnterSomethingDialog(loginFrame, "RSA Authentication",
                                                                    new String[] { lastError, "Enter RSA private key password:" }, true);
                                                    esd.setVisible(true);
                                                    boolean res = conn.authenticateWithPublicKey(username, key, esd.answer);
                                                    if (res == true)
                                                            break;
                                                    lastError = "RSA authentication failed.";
                                            }
                                            enableRSA = false; // do not try again
                                    }
                                    continue;
                            }
                            if (enableKeyboardInteractive && conn.isAuthMethodAvailable(username, "keyboard-interactive"))
                            {
                                    InteractiveLogic il = new InteractiveLogic(lastError);
                                    boolean res = conn.authenticateWithKeyboardInteractive(username, il);
                                    if (res == true)
                                            break;
                                    if (il.getPromptCount() == 0)
                                    {
                                            lastError = "Keyboard-interactive does not work.";
                                            enableKeyboardInteractive = false; // do not try this again
                                    }
                                    else
                                    {
                                            lastError = "Keyboard-interactive auth failed."; // try again, if possible
                                    }

                                    continue;
                            }

                            if (conn.isAuthMethodAvailable(username, "password"))
                            {
                                    final EnterSomethingDialog esd = new EnterSomethingDialog(loginFrame,
                                                    "Password Authentication",
                                                    new String[] { lastError, "Enter password for " + username }, true);
                                    esd.setVisible(true);
                                    if (esd.answer == null)
                                            throw new IOException("Login aborted by user");
                                    boolean res = conn.authenticateWithPassword(username, esd.answer);
                                    if (res == true)
                                            break;
                                    lastError = "Password authentication failed."; // try again, if possible
                                    continue;
                            }
                            throw new IOException("No supported authentication methods available.");
                    }
                    //Session sess2 = conn.openSession();
                    sesja = conn.openSession();
                    int x_width = 90;
                    int y_width = 30;
                    //sess2.requestPTY("dumb", x_width, y_width, 0, 0, null);
                    //sess2.startShell();
                    
                    sesja.requestPTY("dumb", x_width, y_width, 0, 0, null);
                    sesja.startShell();
                    
                    //sesja=sess2;
                    in1 = sesja.getStdout();
                    out1 = sesja.getStdin();
                    //new RemoteConsumer1().start();
                    //td = new TerminalDialog(loginFrame, username + "@" + hostname, sesja, x_width, y_width);
                    td = new TerminalDialog( username + "@" + hostname, sesja, x_width, y_width);
                    td.setVisible(true);
            }
            catch (IOException e)
            {
                    //e.printStackTrace();
                    JOptionPane.showMessageDialog(loginFrame, "Exception: " + e.getMessage());
            }
    }
    static TerminalDialog td;
     static JPanel loginPanel;
     static         JTabbedPane tabbedPane;
    void showGUI()
    {
          //  loginFrame = new JFrame("Test CLI");
            tabbedPane = new JTabbedPane();
            tabbedPane.setSize(200,200);
            hostLabel = new JLabel("Hostname:");
            userLabel = new JLabel("Username:");
            hostField = new JTextField("10.41.93.147", 20);
            userField = new JTextField("log", 10);
            loginButton = new JButton("Login");
            l2 = new JButton("Login2");
            loginPanel = new JPanel();
            loginPanel.add(hostLabel);
            loginPanel.add(hostField);
            loginPanel.add(userLabel);
            loginPanel.add(userField);
            loginPanel.add(loginButton);            loginPanel.add(l2);
            //ComboBoxDemo cb=new ComboBoxDemo();
            //loginPanel.add(cb);
            //loginPanel.add(new JTextField());
            JButton addLab=new JButton("Add command");
            JButton addLab_2=new JButton("Add Lab");
            loginPanel.add(addLab);
            loginPanel.add(addLab_2);
            addLab_2.addActionListener(new ActionListenerPanel("addNewLab"));
            addLab.addActionListener(new ActionListener()
            {
                    public void actionPerformed(java.awt.event.ActionEvent e)
                    {
                            actionAddLab();
                    }
            });
            loginButton.addActionListener(new ActionListener()
            {
                    public void actionPerformed(java.awt.event.ActionEvent e)
                    {
                            loginPressed();
                    }
            });
            l2.addActionListener(new ActionListener()
            {
                    public void actionPerformed(java.awt.event.ActionEvent e)
                    {
                            loginPressed();
                    }
            });
            //loginPanel.add(terminalArea1);
            this.setPreferredSize(new Dimension(1000, 1000));
            this.getRootPane().setDefaultButton(loginButton);
            this.getContentPane().add(loginPanel, BorderLayout.PAGE_START);
            //this.getContentPane().add(textArea, BorderLayout.CENTER);
            this.getContentPane().add(tabbedPane, BorderLayout.CENTER);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.pack();
            //this.setResizable(true);
            //this.setLocationRelativeTo(null);
            this.setVisible(true);
            File_.onlyReadFile(0);
            
    }
    void actionAddLab(){
            new Lab();
    }
    void startGUI()
    {
            Runnable r = new Runnable()
            {
                    public void run()
                    {
                            showGUI();
                    }
            };

            SwingUtilities.invokeLater(r);

    }

    public static void main(String[] args)
    {
            SwingShell client = new SwingShell();
            client.startGUI();
    }
}
